/*_plane.h - planar functions
 *
 * Copyright(c) 2007 Bert van der Weerd<hypercharge@gmail.com>
 *
 * This is Free Software under BSD licence. Share and enjoy.
 */

#ifndef INCLUDED_IONDRIVE_PLANE
#define INCLUDED_IONDRIVE_PLANE

#include <iondrive/glsl.h>

namespace iondrive {
namespace plane {

	using namespace iondrive;



	using glsl::vec3;

	class plane
	{
		
		// The plane is represented as a unit length normal vector and a constant
		
		vec3 normal; float constant;
        
		public:

			plane() : normal(0.0f), constant(0.0f) {}
			plane(const plane& p) { normal = p.normal; constant = p.constant; }
			plane(const vec3& v, const float f) { normal = v; constant = f; }
			plane(const vec3& v1, const vec3& v2, const vec3& v3) // plane defined by 3 points
			{
				normal = normalize(cross(v2-v1,v3-v1));
				constant = dot(normal,v1);
			}

			// member functions
            
			float distance_to(const vec3& v) { return dot(normal,v) - constant; }

			int which_side(const vec3& v) { 
				float tmp = distance_to(v); 
				if (tmp > 0) return 1; if (tmp < 0) return -1; return 0; 
			}
	};


} // namespace plane
} // namespace iondrive

#endif /* INCLUDED_IONDRIVE_PLANE */

